package com.bjsxt.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bjsxt.entity.Customer;
import com.bjsxt.utils.DBUtil;

public class CustomerDao {
	public List<Customer> getCustomers() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Customer> list = new ArrayList<Customer>();

		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn, "select id,userName,gender,birth,tel from t_customer");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Customer c = new Customer(rs.getInt("id"), rs.getString("userName"), rs.getInt("gender"),
						rs.getDate("birth"), rs.getString("tel"));
				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}

		if (list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	public Customer getCustomer(int id) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Customer c = null;

		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn,
					"select id,userName,gender,birth,tel from t_customer where id = ?");
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				c = new Customer(rs.getInt("id"), rs.getString("userName"), rs.getInt("gender"), rs.getDate("birth"),
						rs.getString("tel"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}

		return c;
	}

	public boolean deleteCustomer(int id) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int num = 0;
		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn, "delete from t_customer where id = ?");
			pstmt.setInt(1, id);
			num = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pstmt, conn);
		}
		if (num > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean addCustomer(String userName, int gender, Date birth, String tel) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int num = 0;
		
		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn,
					"insert into t_customer (userName,gender,birth,tel) values (?,?,?,?)");
			pstmt.setString(1, userName);
			pstmt.setInt(2, gender);
			pstmt.setDate(3, birth);
			pstmt.setString(4, tel);

			num = pstmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pstmt, conn);
		}
		if (num > 0) {
			return true;
		} else {
			return false;
		}

	}

	public boolean updateCustomer(int id, String userName, int gender, Date birth, String tel){
		Connection conn = null;
		PreparedStatement pstmt = null;
		int num = 0;
		
		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn, "update t_customer set userName=?,gender=?,birth=?,tel=? where id=?");
						
			pstmt.setString(1, userName);
			pstmt.setInt(2, gender);
			pstmt.setDate(3, birth);
			pstmt.setString(4, tel);
			pstmt.setInt(5, id);
			
			num = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pstmt, conn);
		}
		if(num > 0){
			return true;
		}else{
			return false;
		}
	}
}
